local skill = fk.CreateSkill {
  name = "#he_gem_skill",
  tags = { Skill.Compulsory },
  attached_equip = "he_gem",
}

Fk:loadTranslationTable{
  ["#he_gem_skill"] = "和氏璧",
  ["#he_gem-choose"] = "请将【和氏璧】交给一名其他角色",
}

skill:addEffect(fk.DrawNCards, {
  on_use = function(self, event, target, player, data)
    data.n = data.n + 1
  end,
})

skill:addEffect("maxcards", {
  correct_func = function(self, player)
    if player:hasSkill(skill.name) then
      return 1
    end
  end,
})

skill:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if player.dead or #player.room:getOtherPlayers(player, false) == 0 then return end
    for _, move in ipairs(data) do
      if move.from == player and move.toArea == Card.DiscardPile and Fk.skills[skill.name]:isEffectable(player) then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and
            Fk:getCardById(info.cardId).name == skill.attached_equip and
            table.contains(player.room.discard_pile, info.cardId) then
            return true
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room:getOtherPlayers(player, false),
      skill_name = skill.name,
      prompt = "#he_gem-choose",
      cancelable = false,
    })[1]
    local ids = {}
    for _, move in ipairs(data) do
      if move.from == player and move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and
            Fk:getCardById(info.cardId).name == skill.attached_equip and
            table.contains(player.room.discard_pile, info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end
    room:moveCardTo(ids, Card.PlayerHand, to, fk.ReasonGive, skill.name, nil, true, player)
  end,
})

skill:addEffect(fk.BeforeCardsMove, {
  can_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).name == "he_gem" and
            info.fromArea == Card.PlayerEquip and move.moveReason == fk.ReasonPutIntoDiscardPile then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).name == "he_gem" and
            info.fromArea == Card.PlayerEquip and move.moveReason == fk.ReasonPutIntoDiscardPile then
            player.room:setCardMark(Fk:getCardById(info.cardId), MarkEnum.DestructOutEquip, 1)
          end
        end
      end
    end
  end,
})

return skill
